package com.student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------学生信息管理系统--------");
            System.out.println("1、添加学生信息");
            System.out.println("2、删除学生信息");
            System.out.println("3、修改学生信息");
            System.out.println("4、查看所有学生信息");
            System.out.println("5、退出系统");
            System.out.println("请输入你选择的序号：");
            int choose = sc.nextInt();
            switch (choose){
                case 1:
                    addStudent(students, sc);
                    break;
                case 2:
                    deleteStudent(students, sc);
                    break;
                case 3:
                    updateStudent(students, sc);
                    break;
                case 4:
                    findStudent(students);
                    break;
                case 5:
                    System.out.println("感谢您的使用！");
                    System.exit(0);
            }
        }
    }

    private static void updateStudent(ArrayList<Student> students, Scanner sc) {
        if (students.size() == 0){
            System.out.println("系统暂无学生信息，请先输入学生信息！");
            return;
        }
        System.out.println("请输入你要修改的学号：");
        String sid = sc.next();
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            if (stu.getSid().equals(sid)){
                index = i;
                break;
            }
        }
        if (index ==-1){
            System.out.println("该学生信息不存在，请重新输入！");
        } else {
            System.out.println("请输入学生姓名：");
            String name = sc.next();
            System.out.println("请输入学生年龄：");
            int age = sc.nextInt();
            System.out.println("请输入学生地址：");
            String address = sc.next();
            Student stu = new Student();
            stu.setSid(sid);
            stu.setName(name);
            stu.setAge(age);
            stu.setAddress(address);
            for (int i = 0; i < students.size(); i++) {
                Student stu1 = students.get(i);
                if (stu1.getSid().equals(sid)){
                    students.set(i,stu);
                    break;
                }
            }
            System.out.println("修改学生信息成功！");
        }
    }

    private static void deleteStudent(ArrayList<Student> students, Scanner sc) {
        if (students.size() == 0){
            System.out.println("系统暂无学生信息，请先输入学生信息！");
            return;
        }
        System.out.println("请输入你要删除的学号:");
        String sid = sc.next();
        int index= -1;
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            if (stu.getSid().equals(sid)){
                index = i;
                break;
            }
        }
        if (index == -1){
            System.out.println("该学生信息不存在，请重新输入！");
        } else {
            students.remove(index);
            System.out.println("删除学生信息成功！");
        }
    }

    private static void findStudent(ArrayList<Student> students) {
        if (students.size() == 0){
            System.out.println("系统暂无学生信息，请先输入学生信息！");
            return;
        }
        System.out.println("学号\t\t\t姓名\t\t\t年龄\t\t\t地址");
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t\t" + stu.getAge() + "岁\t\t" + stu.getAddress());
        }
    }

    private static void addStudent(ArrayList<Student> students, Scanner sc){
        Student student = new Student();
        System.out.println("请输入学生学号：");
        String sid = sc.next();
        student.setSid(sid);
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            if (stu.getSid().equals(sid)){
                index = i;
                break;
            }
        }
        if (index != -1){
            System.out.println("该学生信息已存在，请重新输入！");
        } else {
            System.out.println("请输入学生姓名：");
            String name = sc.next();
            System.out.println("请输入学生年龄：");
            int age = sc.nextInt();
            System.out.println("请输入学生地址：");
            String address = sc.next();
            student.setName(name);
            student.setAge(age);
            student.setAddress(address);
            students.add(student);
            System.out.println("添加学生信息成功！");
        }
    }
}
